package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.FileAccess;
import com.zrwl.poscloud.base.entity.IpAuth;
import com.zrwl.poscloud.commons.vo.PageResult;
import com.zrwl.poscloud.xo.mapper.FileAccessMapper;
import com.zrwl.poscloud.xo.mapper.IpAuthMapper;
import com.zrwl.poscloud.xo.service.AccessManageService;
import com.zrwl.poscloud.xo.vo.FileAccessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccessManageServiceImpl extends SuperServiceImpl<FileAccessMapper, FileAccess> implements AccessManageService {

    @Autowired
    AccessManageService accessManageService;
    @Autowired(required = false)
    IpAuthMapper ipAuthMapper;


    //ip授权表
    @Override
    public void enableAccess(String ip) {
        /***
         * 查到最后一条ip的授权信息，如果不存则true，如果存在且type为2时返回false
         */
        QueryWrapper<IpAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ip", ip);
        queryWrapper.ne("status", 0);
        queryWrapper.orderByDesc("create_time");
        List<IpAuth> list = ipAuthMapper.selectList(queryWrapper);
        if (list.size() == 0)
            return;
        if (list.get(0).getType() == 2)
            throw new RuntimeException("您暂时无法访问此资源");
    }

    //文件权限表
    @Override
    public PageResult getFileAccessList(FileAccessVO fileAccessVO) {
        QueryWrapper<FileAccess> queryWrapper = new QueryWrapper<>();
        Page<FileAccess> page = new Page<>();
        page.setCurrent(fileAccessVO.getCurrentPage());
        page.setSize(fileAccessVO.getPageSize());
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("create_time");
        IPage<FileAccess> pageList = page(page, queryWrapper);
        PageResult result = new PageResult();
        result.setRecord(pageList.getRecords());
        result.setTotal(pageList.getTotal());
        return result;
    }

    /***
     * 统计文件的访问结果
     * 如果文件存在访问记录则在原记录的基础上进行+1，否则创建一条访问记录
     * @param fileUid
     * @param result
     * @param ip
     */
    @Override
    public void saveFileAccess(String fileUid, boolean result, String ip) {
        QueryWrapper<FileAccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_info_uid", fileUid);
        FileAccess entity = getOne(queryWrapper);
        if(entity != null){
            if (result) {
                entity.setSuccessCount(entity.getSuccessCount() + 1);
            } else {
                entity.setErrorCount(entity.getErrorCount() + 1);
            }
        }else {
            entity = new FileAccess();
            entity.setIp(ip);
            entity.setFileInfoUid(fileUid);
            if (result) {
                entity.setSuccessCount(1);
            }else {
                entity.setErrorCount(1);
            }
            entity.setStatus(1);
            entity.insert();
        }

    }

}
